/**
 * 
 */
package com.simtek.simplayer.utils;

import java.io.File;

/**
 * @author Administrator
 * 
 */
public class FilenameUtils
{
    public static final char  EXTENSION_SEPARATOR = '.';
    /**
     * The Unix separator character.
     */
    private static final char UNIX_SEPARATOR      = '/';

    /**
     * The Windows separator character.
     */
    private static final char WINDOWS_SEPARATOR   = '\\';

    /**
     * The system separator character.
     */
    private static final char SYSTEM_SEPARATOR    = File.separatorChar;

    public static String getBaseName(String filename)
    {
        return removeExtension(getName(filename));
    }

    public static String getName(String filename)
    {
        if (filename == null)
        {
            return null;
        }
        int index = indexOfLastSeparator(filename);
        return filename.substring(index + 1);
    }

    public static int indexOfLastSeparator(String filename)
    {
        if (filename == null)
        {
            return -1;
        }
        int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR);
        int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
        return Math.max(lastUnixPos, lastWindowsPos);
    }

    public static String removeExtension(String filename)
    {
        if (filename == null)
        {
            return null;
        }
        int index = indexOfExtension(filename);
        if (index == -1)
        {
            return filename;
        }
        else
        {
            return filename.substring(0, index);
        }
    }

    public static int indexOfExtension(String filename)
    {
        if (filename == null)
        {
            return -1;
        }
        int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
        int lastSeparator = indexOfLastSeparator(filename);
        return lastSeparator > extensionPos ? -1 : extensionPos;
    }

    static boolean isSystemWindows()
    {
        return SYSTEM_SEPARATOR == WINDOWS_SEPARATOR;
    }

    public static String getExtension(String filename)
    {
        if (filename == null)
        {
            return null;
        }
        int index = indexOfExtension(filename);
        if (index == -1)
        {
            return "";
        }
        else
        {
            return filename.substring(index + 1);
        }
    }

    public static String getPath(String filename)
    {
        return doGetPath(filename, 1);
    }

    private static String doGetPath(String filename, int separatorAdd)
    {
        if (filename == null)
        {
            return null;
        }
        int prefix = getPrefixLength(filename);
        if (prefix < 0)
        {
            return null;
        }
        int index = indexOfLastSeparator(filename);
        int endIndex = index + separatorAdd;
        if (prefix >= filename.length() || index < 0 || prefix >= endIndex)
        {
            return "";
        }
        return filename.substring(prefix, endIndex);
    }

    public static int getPrefixLength(String filename)
    {
        if (filename == null)
        {
            return -1;
        }
        int len = filename.length();
        if (len == 0)
        {
            return 0;
        }
        char ch0 = filename.charAt(0);
        if (ch0 == ':')
        {
            return -1;
        }
        if (len == 1)
        {
            if (ch0 == '~')
            {
                return 2; // return a length greater than the input
            }
            return isSeparator(ch0) ? 1 : 0;
        }
        else
        {
            if (ch0 == '~')
            {
                int posUnix = filename.indexOf(UNIX_SEPARATOR, 1);
                int posWin = filename.indexOf(WINDOWS_SEPARATOR, 1);
                if (posUnix == -1 && posWin == -1)
                {
                    return len + 1; // return a length greater than the input
                }
                posUnix = posUnix == -1 ? posWin : posUnix;
                posWin = posWin == -1 ? posUnix : posWin;
                return Math.min(posUnix, posWin) + 1;
            }
            char ch1 = filename.charAt(1);
            if (ch1 == ':')
            {
                ch0 = Character.toUpperCase(ch0);
                if (ch0 >= 'A' && ch0 <= 'Z')
                {
                    if (len == 2 || isSeparator(filename.charAt(2)) == false)
                    {
                        return 2;
                    }
                    return 3;
                }
                return -1;

            }
            else if (isSeparator(ch0) && isSeparator(ch1))
            {
                int posUnix = filename.indexOf(UNIX_SEPARATOR, 2);
                int posWin = filename.indexOf(WINDOWS_SEPARATOR, 2);
                if (posUnix == -1 && posWin == -1 || posUnix == 2 || posWin == 2)
                {
                    return -1;
                }
                posUnix = posUnix == -1 ? posWin : posUnix;
                posWin = posWin == -1 ? posUnix : posWin;
                return Math.min(posUnix, posWin) + 1;
            }
            else
            {
                return isSeparator(ch0) ? 1 : 0;
            }
        }
    }

    private static boolean isSeparator(char ch)
    {
        return ch == UNIX_SEPARATOR || ch == WINDOWS_SEPARATOR;
    }
}
